package com.starbucks.opencase.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.data.daPath;
import com.starbucks.opencase.entity.entUserFriend;
import com.starbucks.opencase.entity.entUserInfo;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMMark;
import com.starbucks.opencase.enums.enumCMTip;
import com.starbucks.opencase.function.funFriendMessage;
import com.starbucks.opencase.function.funMessageTest;
import com.starbucks.opencase.function.funSession;
import com.starbucks.opencase.function.funUserTest;
import com.starbucks.opencase.modualr.moFriendMessage;
import com.starbucks.opencase.tool.toolUserFriend;
import com.starbucks.opencase.tool.toolUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

@Service
public class serUserFriend {
    @Autowired
    private toolUserFriend toolUserFriend;
    @Autowired
    private toolUserInfo toolUserInfo;
    @Autowired
    private funSession funSession;
    @Autowired
    private funFriendMessage funFriendMessage;
    @Autowired
    private funUserTest funUserTest;
    @Autowired
    private funMessageTest funMessageTest;
    /**
     * 申请好友
     * @param sid
     * @param message
     */
    public void apply(String sid, JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
        on.put(enumCMKey.mark.name(), enumCMMark.friendApply.ordinal());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(sid,on))return;
        if(!funUserTest.hasUID(sid,message,on))return;
        String uid= funSession.getUIdOfId(sid);
        String to_uid=funUserTest.getUId(message);
        ObjectNode me=om.createObjectNode();
        me.put(enumCMKey.type.name(), 1);
        funFriendMessage.send(uid,to_uid,me);
        on.put(enumCMKey.status.name(), true);
        on.put(enumCMKey.tip.name(), enumCMTip.success_send.getTip());
        funSession.sendMessage(sid,on);
    }

    /**
     * 处理好友申请
     * @param sid
     * @param message
     */
    public void applyActive(String sid,JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
        on.put(enumCMKey.mark.name(), enumCMMark.friendApplyActive.ordinal());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(sid,on))return;
        if(!funUserTest.hasMID(sid,message,on))return;
        if(!funUserTest.hasResult(sid,message,on))return;
        long mid=Long.parseLong(funUserTest.getMId(message));
        boolean result= funUserTest.getResult(message);
        moFriendMessage mo=funFriendMessage.getMessage(mid);
        funFriendMessage.removeMessage(mid);
        if(result) {//同意申请
            toolUserFriend.add(mo.getSender(), mo.getRecipient());
            toolUserFriend.add( mo.getRecipient(),mo.getSender());
        }
        on.put(enumCMKey.status.name(), true);
        on.put(enumCMKey.tip.name(), enumCMTip.success_active.getTip());
        funSession.sendMessage(sid,on);
    }
    /**
     * 查询好友列表
     * @param sid
     * @param message
     */
    public void list(String sid,JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
        on.put(enumCMKey.mark.name(), enumCMMark.friendList.ordinal());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(sid,on))return;
        //获取好友
        List<entUserFriend> fs=toolUserFriend.queryList(funSession.getUIdOfId(sid));
        ArrayNode an=om.createArrayNode();
        ArrayList<ObjectNode> online=new ArrayList<>();
        ArrayList<ObjectNode> not_online=new ArrayList<>();
        for(entUserFriend en:fs){
            entUserInfo info=toolUserInfo.queryInfo(en.getFuid());
            boolean qw=funSession.isOnlineOfUId(info.getUid());
            ObjectNode f=om.createObjectNode();
            f.put(enumCMKey.uid.name(),info.getUid());
            f.put(enumCMKey.nickname.name(), info.getNickname());
            f.put(enumCMKey.gender.name(), info.getGender());
            f.put(enumCMKey.url.name(), daPath.getAvatar(info.getGender()+""));
            f.put(enumCMKey.online.name(), qw);
            if(qw)online.add(f);
            else not_online.add(f);
        }
        for(ObjectNode o:online)an.add(o);
        for(ObjectNode o:not_online)an.add(o);
        on.put(enumCMKey.data.name(), an);
        on.put(enumCMKey.status.name(), true);
        funSession.sendMessage(sid,on);
    }

    /**
     * 获取向别人发送的好友申请列表
     * @param sid
     * @param message
     */
    public void applyTo(String sid,JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
        on.put(enumCMKey.mark.name(), enumCMMark.friendApplyTo.ordinal());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(sid,on))return;
        String uid= funSession.getUIdOfId(sid);
        //获取好友申请>主动
        ArrayNode ann=om.createArrayNode();
        List<moFriendMessage> as=funFriendMessage.queryApplyList(uid);
        for(moFriendMessage m:as){
            entUserInfo info=toolUserInfo.queryInfo(m.getRecipient());
            ObjectNode f=om.createObjectNode();
            f.put(enumCMKey.uid.name(), info.getUid());
            f.put(enumCMKey.nickname.name(), info.getNickname());
            f.put(enumCMKey.gender.name(), info.getGender());
            f.put(enumCMKey.url.name(), daPath.getAvatar(info.getGender()+""));
            f.put(enumCMKey.mid.name(),m.getTimestamp());
            ann.add(f);
        }
        on.put(enumCMKey.data.name(), ann);
        on.put(enumCMKey.status.name(), true);
        funSession.sendMessage(sid,on);
    }

    /**
     * 获取他人发送的好友申请列表
     * @param sid
     * @param message
     */
    public void applyFrom(String sid,JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
        on.put(enumCMKey.mark.name(), enumCMMark.friendApplyFrom.ordinal());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(sid,on))return;
        String uid= funSession.getUIdOfId(sid);

        ArrayNode annn=om.createArrayNode();
        List<moFriendMessage> zx=funFriendMessage.queryApplyFromList(uid);
        for(moFriendMessage m:zx){
            entUserInfo info=toolUserInfo.queryInfo(m.getRecipient());
            ObjectNode f=om.createObjectNode();
            f.put(enumCMKey.uid.name(), info.getUid());
            f.put(enumCMKey.nickname.name(), info.getNickname());
            f.put(enumCMKey.gender.name(), info.getGender());
            f.put(enumCMKey.url.name(), daPath.getAvatar(info.getGender()+""));
            f.put(enumCMKey.mid.name(),m.getTimestamp());
            annn.add(f);
        }
        on.put(enumCMKey.applyFrom.name(), annn);
        on.put(enumCMKey.status.name(), true);
        funSession.sendMessage(sid,on);
    }
    public void send(String sid,JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
        on.put(enumCMKey.mark.name(), enumCMMark.friendSend.ordinal());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(sid,on))return;
        if(!funUserTest.hasUID(sid,message,on))return;
        if(!funUserTest.hasMessage(sid,message,on))return;
        String uid= funSession.getUIdOfId(sid);
        String to_uid=funUserTest.getUId(message);
        String me= new String(Base64.getDecoder().decode(funUserTest.getMessage(message)), StandardCharsets.UTF_8);
        if(!funMessageTest.test(me)){//敏感词检测未通过
            on.put(enumCMKey.tip.name(), enumCMTip.failed_send_of_sensitive.getTip());
            funSession.sendMessage(sid,on);
            return;
        }
        ObjectNode mee=om.createObjectNode();
        mee.put(enumCMKey.type.name(), 0);
        mee.put(enumCMKey.message.name(), Base64.getEncoder().encodeToString(me.getBytes()));
        long mid= funFriendMessage.send(uid,to_uid,mee);
        if(funSession.isOnlineOfUId(to_uid)){//对方在线
            funFriendMessage.removeMessage(mid);
        }
        on.put(enumCMKey.status.name(), true);
        funSession.sendMessage(sid,on);
    }
}
